package com.base.cn.platform.os.service.manage.help;

import com.base.cn.platform.os.common.ManageRedisConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.manage.help.SysHelpCenterDao;
import com.base.cn.platform.os.entity.manage.help.SysHelpCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 帮助中心Service
 *
 * @author s.li
 * @create 2018-03-26 18:52
 */
@Service
public class SysHelpCenterBiz extends BaseBiz<SysHelpCenter,SysHelpCenterDao> {
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 保存帮助文档信息
     * @param helpCenter
     * @return
     */
    public SysHelpCenter saveSysHelpCenter(SysHelpCenter helpCenter){
        //修改
        if(DataUtil.idIsNotNull(helpCenter.getId())){
            SysHelpCenter _helpCenter = this.findById(helpCenter.getId());
            _helpCenter.setHelpName(helpCenter.getHelpName());
            _helpCenter.setShowClients(helpCenter.getShowClients());
            _helpCenter.setFrontBottom(helpCenter.getFrontBottom());
            _helpCenter.setStatus(helpCenter.getStatus());
            _helpCenter.setContext(helpCenter.getContext());
            this.updateById(_helpCenter,null);
            customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT);
            return _helpCenter;
        }else{//添加
            this.save(helpCenter);
            customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT);
        }
        customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT);
        return helpCenter;
    }

    /**
     * 删除帮助文档
     * @param ids ID串
     */
    public void deleteSysHelpCenter(String ids){
        if(StringUtils.isNotEmpty(ids)){
            ids = StringUtils.subHeadTailString(ids,",");
            if(StringUtils.isNotEmpty(ids)){
                this.deleteWhereSql(" id in ("+ids+") or parentId in ("+ids+")");
                customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT);
            }
        }
    }

    /**
     * 获取前台底部帮助文档数据
     * @param helpCenter
     * @return List<SysHelpCenter>
     */
    public List<SysHelpCenter> findWebFooterHelp(SysHelpCenter helpCenter){
        List<SysHelpCenter> helpCenterList = (List<SysHelpCenter>)customCacheChannel.get(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT).getValue();
        if(ObjectUtils.isEmpty(helpCenterList)){
            helpCenterList = findSysHelpCenter(helpCenter,false);
            helpCenterList = this.handleLayered(helpCenterList);
            if(helpCenterList!=null){
                customCacheChannel.set(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT,helpCenterList);
            }
        }
        return helpCenterList;
    }

    /**
     * 获取页面帮助中心数据
     * @param helpCenter
     * @param contextData
     * @return
     */
    public List<SysHelpCenter> findLayeredPageHelpList(SysHelpCenter helpCenter, boolean contextData){
        List<SysHelpCenter> sysHelpCenterList = findSysHelpCenter(helpCenter,contextData);
        return this.handleLayered(sysHelpCenterList);
    }

    /**
     * 获取
     * @param helpCenter
     * @param contextData 是否需要内容数据
     * @return
     */
    public List<SysHelpCenter> findSysHelpCenter(SysHelpCenter helpCenter, boolean contextData){
        List<String> columnsList = new ArrayList<>();
        columnsList.add("id");
        columnsList.add("helpName");
        columnsList.add("showClients");
        columnsList.add("parentId");
        columnsList.add("frontBottom");
        columnsList.add("status");
        if(contextData){
            columnsList.add("context");
        }
        List<SysHelpCenter> helpCenterList = this.find(this.getWhereSql(helpCenter),null,columnsList);
        return helpCenterList;
    }

    /**
     * 修改帮助文档状态
     * @param ids
     * @param status 1正常，2隐藏
     */
    public void updateHelpStatus(String ids,int status){
        if(StringUtils.isNotEmpty(ids)){
            ids = StringUtils.subHeadTailString(ids,",");
            if(StringUtils.isNotEmpty(ids)){
                SysHelpCenter sysHelpCenter = new SysHelpCenter();
                sysHelpCenter.setStatus(status);
                this.updateByWhereSql(sysHelpCenter," id in ("+ids+") or parentId in ("+ids+")");
                customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),ManageRedisConstants.WEB_FOOTER_HELP_CONTENT);
            }
        }
    }

    //=====================================

    /**
     * 分成处理
     * @param helpCenterList
     * @return List<SysHelpCenter>
     */
    private List<SysHelpCenter> handleLayered(List<SysHelpCenter> helpCenterList){
        if(ObjectUtils.isNotEmpty(helpCenterList)){
            Map<BigDecimal,List<SysHelpCenter>> helpCenterListMap = helpCenterList.stream().collect(Collectors.groupingBy(e->e.getParentId()));
            List<SysHelpCenter> parentList = helpCenterListMap.get(new BigDecimal(0));
            if(ObjectUtils.isNotEmpty(parentList)){
                parentList.forEach(parent ->{
                    List<SysHelpCenter> childList = helpCenterListMap.get(parent.getId());
                    parent.setChildHelpList(childList);
                });
            }
            return parentList;
        }
        return helpCenterList;
    }

    private String getWhereSql(SysHelpCenter helpCenter){
        StringBuilder whereSql = new StringBuilder( " 1=1 ");
        if(ObjectUtils.isNotEmpty(helpCenter)){
            if(ObjectUtils.isNotEmpty(helpCenter.getStatus()) && helpCenter.getStatus().intValue()>0){
                whereSql .append(" and status=").append(helpCenter.getStatus());
            }
            if(helpCenter.getParentId()!=null){
                whereSql .append(" and parentId = ").append(helpCenter.getParentId());
            }
            if(ObjectUtils.isNotEmpty(helpCenter.getShowClients()) && helpCenter.getShowClients().intValue()>0){
                whereSql .append(" and showClients = ").append(helpCenter.getShowClients());
            }
            if(ObjectUtils.isNotEmpty(helpCenter.getFrontBottom()) && helpCenter.getFrontBottom().intValue()>0){
                whereSql .append(" and frontBottom = ").append(helpCenter.getFrontBottom());
            }
        }
        return whereSql.toString();
    }
}
